//https://leetcode.cn/problems/search-insert-position/submissions/?envType=study-plan-v2&envId=top-100-liked

#include <vector>
#include <stack>
#include <list>
#include <map>
#include <string>
#include <unordered_map>

using namespace std;

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int l = 0, r = nums.size();
        while(l < r){
            // 二分求中点
            int m = l+(r-l)/2;
            if(nums[m] >= target)
                r = m;
            else
                l = m+1;
        }
        // 返回左边界
        return l;
    }
};

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int location=0;
        int left=0;
        int right=nums.size();
        while(left<right){
            int mid=left+(right-left)/2;
            if(nums[mid]==target){
                location=mid;
                break;
            }else if(nums[mid]>target){
                right=mid;
                location=left;
            }else{
                left=mid+1;
                location=left;
            }
        }
        return location;
    }
};